import {Component, OnInit, ViewChild} from '@angular/core';
import {IonicPage, Navbar, NavController, ViewController} from 'ionic-angular';
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";
import {AddressProvider} from "@providers/address";
import {HttpProvider} from "@providers/http";
import {Util} from "@providers/util";
import {NativeProvider} from "@providers/natives/native";

/**
 * Generated class for the WorkerRegisterPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-worker-register',
  templateUrl: 'worker-register.html',
})
export class WorkerRegisterPage implements OnInit {
  @ViewChild(Navbar) navbar: Navbar;
  name: any;
  tel: any;
  RYLX: any[] = [];
  Password: number;
  checkPassword: number;
  IDnumber: any;
  Branch: any;
  is_app: boolean;
  Branchdata: any[] = [];
  userInfo: any = this.storage.read("userInfo");
  picList: any[] = []; //手机选择的图片
  address2: any;
  addressId2: any = [];
  xxdz: string;
  district1: any;
  SHBJ: any;
  read: boolean = false;
  tip: any;
  bz: any;
  bxgs: string;//保险公司
  bxdh: string;//保险单号
  //ionic中的ion-datetime得到的时间为iso标准是一个字符串
  bx_start: string;//保险开始时间
  bx_end: string;//保险结束时间
  max: any;//选择保险时间的最大值
  min: any;//选择保险时间的最小值

  constructor(private viewCtrl: ViewController, private dialog: DialogProvider,
              private storage: StorageProvider, public navCtrl: NavController,
              private address: AddressProvider,
              private native: NativeProvider,
              private http: HttpProvider) {
    this.is_app = this.native.isCordova;
  }

  isnull() {

    if (this.read) {
      return
    }

    if (!this.name) {
      this.dialog.showAlert("姓名不允许为空！");
      return;
    }

    if (!this.tel) {
      this.dialog.showAlert("电话不允许为空！");
      return;
    } else {
      if (!/^1[0-9]{10}$/.test(this.tel)) {
        this.dialog.showAlert('请输入正确的手机号码');
        return;
      }
    }

    if (!this.RYLX) {
      this.dialog.showAlert("人员类型不允许为空！");
      return;
    }

    if (!this.bxgs) {
      this.dialog.showAlert('请输入正确的保险公司名称');
      return;
    }

    if (!this.bxdh) {
      if (!this.bxdh.match('[0-9]{10,20}')) {
        this.dialog.showAlert('请输入正确的保险单号');
        return;
      }
    }

    if (!this.bx_start || !this.bx_end) {
      this.dialog.showAlert('保险开始或结束时间不能为空');
      return;
    } else {
      let start = this.stringToDate(this.bx_start, '-');
      let end = this.stringToDate(this.bx_end, '-');
      if (end < start) {
        this.dialog.showAlert('保险结束时间大于保险开始时间，请重新输入');
        return;
      }
    }

    if (!this.IDnumber) {
      this.dialog.showAlert("身份证号不允许为空！");
      return;
    } else {
      if (!/(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/.test(this.IDnumber)) {
        this.dialog.showAlert('请输入正确的身份证号码');
        return;
      }
    }

    if (!this.address2) {
      this.dialog.showAlert("地址不允许为空！");
      return;
    }

    if (!this.xxdz) {
      this.dialog.showAlert('详细地址不能为空');
      return;
    } else {
      if (this.xxdz.toString().length >= 50) {
        this.dialog.showAlert('详细地址最大长度不能大于50');
        return;
      }
    }

    this.submit();
  }

  getBranch() {
    this.http.post("/Inbound/invoke.do",
      {
        interfaceId: "csm.css.jcdy.qcfwquery.wd",
        data: JSON.stringify({"LX": this.RYLX})
      }).subscribe(
      message => {
        this.Branchdata = JSON.parse(message).data;
      },
      error => {
        this.dialog.showAlert("请选择人员类型");
      });
  }

  //选择地址
  chooseAddress() {
    if (this.read) {
      return
    }
    this.address.selectAddress((addressList) => {
      this.address2 = "";
      this.addressId2 = addressList;
      for (var i = 0; i < addressList.length; i++) {
        this.address2 += addressList[i].VALUE + " ";
      }
    });
  }

  selectAddress2(e: string) {
    this.addressId2 = [e['省'], e['市'], e['区'], e['街道']];
  }

  submit() {
    console.info(this.RYLX, "成功");
    this.http.post("/Inbound/invoke.do",
      {
        // interfaceId: "csm.css.jcdy.qcfwdefjk.fwry.insert",
        interfaceId: "csm.som.jcdy.qcfwdef.insertfwry",
        data: JSON.stringify({
          "FWRY02": this.name,
          "LXDH": this.tel,
          // "FWLX": this.RYLX,
          // "DLMM": this.Password,
          "SFZH": this.IDnumber,
          // "FWWD01": this.Branch,
          // "PHOTO": this.picList,
          // "RYZT": 3
          "RYLX": this.RYLX,
          "XXDZ": this.address2 + this.xxdz,
          "SHBJ": this.SHBJ,
          "BXGS": this.bxgs,
          "BXDH": this.bxdh,
          "BX_START": this.bx_start,
          "BX_END": this.bx_end,
        }),
        rmstoken: '127.0.0.1'
      }).subscribe(
      message => {
        this.dialog.showAlert("提交成功,请耐心等待管理员审核!");
        this.viewCtrl.dismiss();
      },
      error => {
        this.dialog.showAlert("提交失败");
      });
  }

  blurInput() {
    if (this.tel) {
      if (!/^1[0-9]{10}$/.test(this.tel)) {
        this.dialog.showAlert('请输入正确的手机号码');
        // console.info(this.Password.toString().length);
        return;
      }
    }

    this.http.post("/Inbound/invoke.do",
      {
        interfaceId: "csm.css.jcdy.qcfwquery.selectLXDH",
        data: JSON.stringify({
          "LXDH": this.tel
        }), rmstoken: '127.0.0.1'
      }).subscribe(
      message => {
        // this.dialog.showToast(JSON.parse(message)['data'][0]['LXDH']);/*JSON.parse(message)['data'][0]['LXDH']*/
        if (!Util.isNull(message.data)) {
          let datas = message.data;
          this.name = datas['NAME'];
          this.tel = datas['TEL'];
          this.IDnumber = datas['IDNUMBER'];
          this.RYLX = datas['RYLX'];
          this.address2 = datas['address2'];
          this.xxdz = datas['xxdz'];
          this.SHBJ = datas['SHBJ'];
          this.bz = datas['SHBZ'];
          this.read = false;
          if (this.SHBJ == '1') {
            this.read = true;
            this.tip = "已审核";
          } else if (this.SHBJ == '2') {
            this.tip = "审核不通过";
          } else {
            this.tip = "未审核";
          }
        } else {
          this.SHBJ = '0';
          this.read = false;
          this.tip = '';
        }
      },
      error => {
      });
  }

  //字符串转日期
  stringToDate(dateStr: string, separator: string): Date {
    if (!separator) {
      separator = "-";
    }
    let dateArr = dateStr.split(separator);
    let year = parseInt(dateArr[0]);
    let month;
    //处理月份为04这样的情况
    if (dateArr[1].indexOf("0") == 0) {
      month = parseInt(dateArr[1].substring(1));
    } else {
      month = parseInt(dateArr[1]);
    }
    let day = parseInt(dateArr[2]);
    let date = new Date(year, month, day);
    return date;
  }

  ngOnInit(): void {
    this.max = new Date().getFullYear() + 10;
    this.min = new Date().getFullYear() - 10;
  }

  imageChangList() {
  }
}
